package org.come.Jpanel;

import com.tool.btn.*;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;
import org.apache.commons.lang.StringUtils;
import org.come.bean.MyLiangHaoAucBean;
import org.come.bean.SearchSellLiangHaoResultBean;
import org.come.bean.SearchSellXianYuResultBean;
import org.come.entity.SellLiangHaoBase;
import org.come.entity.SellLianghaoAuc;
import org.come.model.Eshop;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.GsonUtil;
import org.come.until.SrcollPanelUI;
import org.come.until.Util;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

public class GetLiangHaoTwoJpanel extends JPanel {

    private static final long serialVersionUID = 1L;
    ImageIcon bg1,zttiao,fengexian;
    private LiangHaoPaiBtn liangHaoPaiBtn;//我的竞拍
    private JLabel name;

    private JLabel labName;

    private String preViewLiangHao = "888888";

    private LiangHaoItemBtn[] liangHaoListBtn;

    private LiangHaoItemBtn[] liangHaoViewListBtn;

    private LiangHaoItemBtn[] aucProcessListBtn;

	ImageIcon itemBg;

    private SearchSellLiangHaoResultBean alb;

    //我的竞价表格
    private JTable tableLiangHaoList;
    private DefaultTableModel tableModel;
    private Vector<Vector<String>> verVectors;
    private Color foreground, background;
    private BigDecimal selectSellId;
    private int tab_row = -1;// 点击的行

    private LiangHaoItemBtn btnsyy,// 上一页
                            btnxyy;// 下一页
    private int nowpage = 1;// 当前页数
    private JLabel labpage; // 放置当前页数和总页数

    private List<SellLiangHaoBase> nLhList = new ArrayList<>();
    private boolean isMylist = false;


    MyLiangHaoAucBean myLiangHaoAucBean;
    private int my_tab_row = -1;// 点击的行1
    private int my_nowpage = 1;// 当前页数
    private List<SellLianghaoAuc> my_AucList = new ArrayList<>();
    private Color color = new Color(56, 53, 46, 238);
    //
    public GetLiangHaoTwoJpanel() {
        this.setPreferredSize(new Dimension(697, 538));
        this.setLayout(null);
        this.setOpaque(false);
        this.bg1 = new ImageIcon("inkImg/newbackground/LiangHaoOneBg2.png");
        this.itemBg = new ImageIcon("inkImg/newbutton/199.png");
        this.zttiao = new ImageIcon("inkImg/newbackground/jingpaizhuangtai.png");
        this.fengexian = new ImageIcon("inkImg/newbutton/fengexian.png");

        liangHaoPaiBtn = new LiangHaoPaiBtn("inkImg/newbutton/LiangHaoPaiBtn.png", 1, UIUtils.COLOR_WHITE, UIUtils.TXT_hyzjt18, "我的竞拍", 2, this);
        this.liangHaoPaiBtn.setBounds(540, 80, 112, 43);
        this.add(this.liangHaoPaiBtn);

        name = new JLabel();
        name.setBounds(80, 93, 120, 15);
        name.setForeground(Color.BLACK);
        name.setFont(new Font("宋体", 0, 16));
        name.setText("竞拍时间为");
        this.add(name);

        labName = new JLabel();
        labName.setBounds(180, 93, 400, 15);
        labName.setForeground(Color.RED);
        labName.setFont(new Font("宋体", 0, 16));
        this.add(labName);

		this.liangHaoListBtn = new LiangHaoItemBtn[6];
		for (int i = 1; i <= 6; i++) {
			liangHaoListBtn[i-1] = new LiangHaoItemBtn("inkImg/newbutton/187.png",1,2,"竟拍",UIUtils.COLOR_WHITE);
			int line = (i-1)/2;
			int index = (i-1)%2;
			liangHaoListBtn[i-1].setBounds(272 + 318 * index,187 + 115 * line,67,35);
            //liangHaoListBtn[i-1].setFont(UIUtils.TEXT_FONT);
			this.add(liangHaoListBtn[i-1]);
		}

        this.liangHaoViewListBtn = new LiangHaoItemBtn[6];
        for (int i = 1; i <= 6; i++) {
            liangHaoViewListBtn[i-1] = new LiangHaoItemBtn("inkImg/newbutton/190.png",1,7,"预览",UIUtils.COLOR_HUI);
            int line = (i-1)/2;
            int index = (i-1)%2;
            liangHaoViewListBtn[i-1].setBounds(212 + 318 * index,192 + 115 * line,49,24);
            this.add(liangHaoViewListBtn[i-1]);
        }

        this.aucProcessListBtn = new LiangHaoItemBtn[8];
        for (int i = 1; i <= 8; i++) {
            aucProcessListBtn[i-1] = new LiangHaoItemBtn("inkImg/newbutton/190.png",1,8,"处理",UIUtils.COLOR_HUI);
            aucProcessListBtn[i-1].setBounds(565,143 + 40 * i,49,24);
            this.add(aucProcessListBtn[i-1]);
        }

        // 上一页
        btnsyy = new LiangHaoItemBtn("inkImg/newbutton/156.png", 1, "", 4, this);
        btnsyy.setBounds(310, 493, 18, 25);
        this.add(btnsyy);

        // 下一页
        btnxyy = new LiangHaoItemBtn("inkImg/newbutton/155.png", 1, "", 5, this);
        btnxyy.setBounds(392, 493, 18, 25);
        this.add(btnxyy);

        //TODO 初始化我的竞价表格
        tableModel = new DefaultTableModel();
        // 循环生成测试数据
        verVectors = new Vector<Vector<String>>();

        tableModel.setDataVector(verVectors, Util.vector4);
        // 循环生成测试数据 end
        tableLiangHaoList = new JTable() {
            @Override
            public boolean isCellEditable(int row, int column) {
                // TODO Auto-generated method stub
                return false;
            }
        };
        tableLiangHaoList.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent event) {
//                tab_row = tableLiangHaoList.rowAtPoint(event.getPoint());
////					System.out.println("=============点击的行是：" + tab_row);
//                Object sellId = tableLiangHaoList.getModel().getValueAt(tab_row, 0);
//                if (sellId != null) {
//                    selectSellId = new BigDecimal(sellId.toString());
//                } else {
//                    selectSellId = BigDecimal.ZERO;
//                }
            }
        });
        tableLiangHaoList.setOpaque(false);
        tableLiangHaoList.setShowGrid(false);// 不显示网格

        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                           boolean hasFocus, int row, int column) {

                if (column == 0 || column == 1) {
                    foreground = Color.BLACK;
                } else if (column == 2 || column == 3) {
                    if (value.toString().length() < 5) {
                        foreground = Color.BLACK;
                    } else if (value.toString().length() < 6) {
                        foreground = new Color(36, 219, 118);
                    } else if (value.toString().length() < 7) {
                        foreground = new Color(253, 68, 221);
                    } else if (value.toString().length() < 8) {
                        foreground = new Color(251, 217, 50);
                    } else if (value.toString().length() < 9) {
                        foreground = new Color(0, 239, 239);
                    } else if (value.toString().length() < 10) {
                        foreground = Color.GREEN;
                    } else {
                        foreground = Color.RED;
                    }
                }
                background = UIUtils.Color_BACK;
                if (isSelected) {
                    foreground = Color.GREEN;
                }
                setBackground(background);

                setForeground(foreground);
                if (column == 2) {
                    StringBuffer gold = new StringBuffer(value.toString());
                    int index;
                    for (index = gold.length() - 3; index > 0; index -= 3) {
                        gold.insert(index, ',');
                    }
                    value = gold.toString();
                } else if (column == 1) {
                    if (value.toString().length() < 2) {
                        value = value.toString() + "     ";
                    } else if (value.toString().length() < 3) {
                        value = value.toString() + "    ";
                    } else if (value.toString().length() < 4) {
                        value = value.toString() + "   ";
                    } else if (value.toString().length() < 5) {
                        value = value.toString() + "  ";
                    } else if (value.toString().length() < 6) {
                        value = value.toString() + " ";
                    }
                }

                if (column == 3) {
                    value = value.toString() + "   ";
                }

                return super.getTableCellRendererComponent(table, value, false, hasFocus, row, column);
            }
        };
        renderer.setHorizontalAlignment(SwingConstants.RIGHT);
        tableLiangHaoList.setDefaultRenderer(Object.class, renderer);

        JTableHeader header = tableLiangHaoList.getTableHeader();

        // 设置表头的高度
        header.setPreferredSize(new Dimension(header.getWidth(), 0));
        tableLiangHaoList.getTableHeader().setBackground(new Color(0, 0, 0, 0));
        tableLiangHaoList.getTableHeader().setVisible(true);
        tableLiangHaoList.getTableHeader().setBorder(BorderFactory.createLineBorder(Color.ORANGE));

        tableLiangHaoList.setSelectionBackground(new Color(0, 0, 0, 0));
        tableLiangHaoList.setSelectionForeground(Color.white);
        tableLiangHaoList.setForeground(Color.white);
        tableLiangHaoList.setFont(new Font("楷体", 1, 18));
        tableLiangHaoList.setBackground(UIUtils.Color_BACK);
        tableLiangHaoList.setModel(tableModel);

        tableLiangHaoList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        // 设置列间距
        tableLiangHaoList.getColumnModel().getColumn(0).setPreferredWidth(0);
        tableLiangHaoList.getColumnModel().getColumn(1).setPreferredWidth(50);
        tableLiangHaoList.getColumnModel().getColumn(2).setPreferredWidth(140);
        tableLiangHaoList.getColumnModel().getColumn(3).setPreferredWidth(100);
        tableLiangHaoList.getColumnModel().getColumn(4).setPreferredWidth(150);
        // 设置行高
        tableLiangHaoList.setRowHeight(40);
        tableLiangHaoList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tableLiangHaoList.isCellEditable(1, 1);
        tableLiangHaoList.setEnabled(true);
        tableLiangHaoList.removeColumn(tableLiangHaoList.getColumnModel().getColumn(0));//隐藏某列
        tableLiangHaoList.setBounds(66, 175, 600, 360);
        this.add(tableLiangHaoList);

        this.tableLiangHaoList.setVisible(false);           //默认不显示


//        labpage = new JLabel("", SwingConstants.CENTER);
//        labpage.setForeground(Color.white);
//        labpage.setBounds(320, 485, 58, 17);
//        labpage.setFont(UIUtils.TEXT_FONT1);
//        this.add(labpage);
        Font font = new Font("微软雅黑", 0, 12);
        labpage = new JLabel() {
            @Override
            protected void paintComponent(final Graphics g) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                RoundRectangle2D roundedRectangle = new RoundRectangle2D.Double(0, 0, this.getWidth(), this.getHeight(), 5, 5);
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
                g2d.setColor(color);
                g2d.fill(roundedRectangle);
                g2d.setColor(Color.white);
                super.paintComponent(g);
            }
        };
        labpage.setVerticalAlignment(SwingConstants.CENTER);
        labpage.setHorizontalAlignment(SwingConstants.CENTER);
        SwingUtilities.invokeLater(() -> {
            labpage.repaint();
        });
        labpage.setForeground(Color.white);
        labpage.setBackground(UIUtils.Color_BACK);
        labpage.setBorder(BorderFactory.createEmptyBorder());
        labpage.setFont(font);
        labpage.setBounds(330, 496, 58, 17);
        this.add(labpage);
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(this.bg1.getImage(), 45, 65, 630, 70, null);

        if(this.isMylist){
            g.drawImage(this.zttiao.getImage(), 45, 148, 630, 25, null);
            g.drawImage(this.fengexian.getImage(), 45, 140, 630, 2, null);
        }else{
            g.drawImage(this.fengexian.getImage(), 45, 140, 630, 2, null);
        }
        if(alb!=null){
            refreshSellList(alb.getSellLiangHaos(),nowpage);
        }
        if(isMylist && myLiangHaoAucBean!=null){
            refreshMyAucList(myLiangHaoAucBean.getSellLianghaoAucs(),my_nowpage);
        }

        if(!isMylist && this.aucProcessListBtn.length >0 ){
            for(LiangHaoItemBtn btn:this.aucProcessListBtn){
                btn.setVisible(false);
            }
        }

		if(!this.isMylist && this.nLhList!=null && nLhList!=null && nLhList.size()>0){
			for(int i=1; i<= nLhList.size(); i++){
				int line = (i-1)/2;
				int index = (i-1)%2;
				g.drawImage(this.itemBg.getImage(),45 + 318 * index,148 + 115 * line,313,110,null);
                g.setColor(new Color(79,79,79));
                g.setFont(new Font("汉仪中楷简",1,22));
				g.drawString( nLhList.get(i-1).getLianghao(),70 + 318 * index,188 + 115 * line);
                g.setFont(new Font("汉仪中楷简",0,14));
                g.setColor(new Color(0,0,0));
                g.drawString( "预购数：",70 + 318 * index,213 + 115 * line);
                g.setColor(new Color(255, 0, 0));
                g.drawString( "0",130 + 318 * index,214 + 115 * line);
                g.setColor(new Color(0,0,0));
				g.drawString( "竞拍价：",70 + 318 * index,235 + 115 * line);
                g.setColor(new Color(255, 0, 0));
                if(nLhList.get(i-1).getAucPrice()>nLhList.get(i-1).getPrice()){
                    g.drawString(String.valueOf(nLhList.get(i-1).getAucPrice()),130 + 318 * index,236 + 115 * line);
                }else{
                    g.drawString(String.valueOf(nLhList.get(i-1).getPrice()),130 + 318 * index,236 + 115 * line);
                }
                if(StringUtils.isBlank(labName.getText())){
                    labName.setText(nLhList.get(i-1).getAucStartTime()+" 00:00:00 ~ "+nLhList.get(i-1).getAucEndTime()+" 00:00:00");
                }
				this.liangHaoListBtn[i-1].setLianghaoItem(nLhList.get(i-1));
                this.liangHaoViewListBtn[i-1].setLianghaoItem(nLhList.get(i-1));
			}
		}

		for(int j=0;j<liangHaoListBtn.length;j++){
			if(liangHaoListBtn[j].getLianghaoItem() == null || StringUtils.isEmpty(liangHaoListBtn[j].getLianghaoItem().getLianghao()) || j>=nLhList.size() || isMylist){
				this.liangHaoListBtn[j].setVisible(false);
			}else{
                this.liangHaoListBtn[j].setVisible(true);
            }
		}

        for(int j=0;j<liangHaoViewListBtn.length;j++){
            if(liangHaoViewListBtn[j].getLianghaoItem() == null || StringUtils.isEmpty(liangHaoViewListBtn[j].getLianghaoItem().getLianghao()) || j>=nLhList.size() || isMylist){
                this.liangHaoViewListBtn[j].setVisible(false);
            }else{
                this.liangHaoViewListBtn[j].setVisible(true);
            }
        }
    }

    public void changeFrom(int caozuo) {
        // 向服务器请求寄售点卡列表数据
    }

    public SearchSellLiangHaoResultBean getAlb() {
        return alb;
    }

    public void setAlb(SearchSellLiangHaoResultBean alb) {

        for (int j = 0; j < liangHaoListBtn.length; j++) {
            this.liangHaoListBtn[j].setLianghaoItem(null);
        }
        this.alb = alb;
    }

    public void changeContent(){
        if(!this.isMylist){
            this.isMylist = true;
            this.tableLiangHaoList.setVisible(true);
            this.liangHaoPaiBtn.setText("返回竞拍");

            for(LiangHaoItemBtn btn:this.liangHaoListBtn){
                btn.setVisible(false);
            }
            //获取我的竞拍数据
            String mes = Agreement.getAgreement().selllianghaoAgreement("MYAUCLIST");
            SendMessageUntil.toServer(mes);
        }else{
            this.isMylist = false;
            this.tableLiangHaoList.setVisible(false);
            this.liangHaoPaiBtn.setText("我的竞拍");
            for(LiangHaoItemBtn btn:this.liangHaoListBtn){
                btn.setVisible(true);
            }
        }

    }

    public LiangHaoPaiBtn getLiangHaoPaiBtn() {
        return liangHaoPaiBtn;
    }

    public void setLiangHaoPaiBtn(LiangHaoPaiBtn liangHaoPaiBtn) {
        this.liangHaoPaiBtn = liangHaoPaiBtn;
    }

    public int getNowpage() {
        return nowpage;
    }

    public void setNowpage(int nowpage) {
        this.nowpage = nowpage;
    }

    public void refreshSellList(List<SellLiangHaoBase> lhlist, int nowpage) {
        int totalpage = lhlist.size() / 6 + (lhlist.size() % 6 == 0 ? 0 : 1);
        labpage.setText(nowpage + "/" + totalpage);
        nLhList.clear();
        // 展示的特效信息
        for (int i = 0; i < ((lhlist.size() - (nowpage - 1) * 6) >= 6 ? 6 : (lhlist.size() - (nowpage - 1) * 6)); i++) {
            nLhList.add(lhlist.get(i+ (nowpage - 1) * 6));
        }
    }

    public void refreshMyAucList(List<SellLianghaoAuc> auclist, int nowpage) {
        int totalpage = auclist.size() / 8 + (auclist.size() % 8 == 0 ? 0 : 1);
        labpage.setText(nowpage + "/" + totalpage);
        this.verVectors.clear();


        int i = 0;
        for (i = 0; i < ((auclist.size() - (nowpage - 1) * 8) >= 8 ? 8 : (auclist.size() - (nowpage - 1) * 8)); i++) {

            this.aucProcessListBtn[i].setVisible(true);
            SellLianghaoAuc cslc = myLiangHaoAucBean.getSellLianghaoAucs().get(i+ (nowpage - 1) * 8);
            this.aucProcessListBtn[i].setSellLianghaoAuc(cslc);
            Vector<String> verStrings = new Vector<String>();
            verStrings.add(String.valueOf(cslc.getId()));
            verStrings.add(String.valueOf(cslc.getLianghao()));
            verStrings.add(String.valueOf(cslc.getAucPoint()));
            int status = cslc.getStatus();
            String statusStr = "";
            if(status == 1){
                this.aucProcessListBtn[i].setText("放弃");
                statusStr = "进行中";
            }else if(status == 2){
                this.aucProcessListBtn[i].setVisible(false);
                statusStr = "竞拍成功";
            }else if(status == 3){
                this.aucProcessListBtn[i].setText("重拍");
                statusStr = "已失败";
            }else if(status == 4){
                this.aucProcessListBtn[i].setText("重拍");
                statusStr = "已取消";
            }else if(status == 5){
                this.aucProcessListBtn[i].setText("重拍");
                statusStr = "已失败";
            }
            verStrings.add(statusStr);
            verStrings.add("");
            this.verVectors.add(verStrings);
        }
        for(;i<8;i++){
            this.aucProcessListBtn[i].setVisible(false);
        }
        this.tableModel.fireTableDataChanged();
    }

    public boolean isMylist() {
        return isMylist;
    }

    public void setMylist(boolean mylist) {
        isMylist = mylist;
    }

    public int getMy_tab_row() {
        return my_tab_row;
    }

    public void setMy_tab_row(int my_tab_row) {
        this.my_tab_row = my_tab_row;
    }

    public int getMy_nowpage() {
        return my_nowpage;
    }

    public void setMy_nowpage(int my_nowpage) {
        this.my_nowpage = my_nowpage;
    }

    public MyLiangHaoAucBean getMyLiangHaoAucBean() {
        return myLiangHaoAucBean;
    }

    public void setMyLiangHaoAucBean(MyLiangHaoAucBean myLiangHaoAucBean) {
        this.myLiangHaoAucBean = myLiangHaoAucBean;
    }

    public String getPreViewLiangHao() {
        return preViewLiangHao;
    }

    public void setPreViewLiangHao(String preViewLiangHao) {
        this.preViewLiangHao = preViewLiangHao;
    }


    public List<SellLiangHaoBase> getnLhList() {
        return nLhList;
    }

    public void setnLhList(List<SellLiangHaoBase> nLhList) {
        this.nLhList = nLhList;
    }
}
